/**
 * Created by luguosui on 2019/10/9.
 */

//自定义js

//公共配置


$(document).ready(function () {


    // MetsiMenu
    $('#side-menu').metisMenu();

    // 打开右侧边栏
    $('.right-sidebar-toggle').click(function () {
        $('#right-sidebar').toggleClass('sidebar-open');
    });

    //固定菜单栏
    $(function () {
        $('.sidebar-collapse').slimScroll({
            height: '100%',
            railOpacity: 0.9,
            alwaysVisible: false
        });
    });


    // 菜单切换
    $('#LAY_app_flexible').click(function () {
        $("body").toggleClass("mini-navbar");
        $(this).toggleClass("layui-icon-spread-left");
        SmoothlyMenu();
    });


    // 侧边栏高度
    function fix_height() {
        var heightWithoutNavbar = $("body > #wrapper").height() - 61;
        $(".sidebard-panel").css("min-height", heightWithoutNavbar + "px");
    }
    fix_height();

    $(window).bind("load resize click scroll", function () {
        if (!$("body").hasClass('body-small')) {
            fix_height();
        }
    });

    //侧边栏滚动
    $(window).scroll(function () {
        if ($(window).scrollTop() > 0 && !$('body').hasClass('fixed-nav')) {
            $('#right-sidebar').addClass('sidebar-top');
        } else {
            $('#right-sidebar').removeClass('sidebar-top');
        }
    });

    $('.full-height-scroll').slimScroll({
        height: '100%'
    });

    $('#side-menu>li').click(function () {
        if ($('body').hasClass('mini-navbar')) {
            NavToggle();
        }
    });
    $('#side-menu>li li a').click(function () {
        if ($(window).width() < 769) {
            NavToggle();
        }
    });

    $('.nav-close').click(NavToggle);

    //ios浏览器兼容性处理
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        $('#content-main').css('overflow-y', 'auto');
    }

    //根据ID获取iframe对象
    var ifr = document.getElementById('J_iframe')
    if(ifr!=null)
    {
        ifr.onload = function() {
            //解决打开高度太高的页面后再打开高度较小页面滚动条不收缩
            ifr.style.height='0px';
            var iDoc = ifr.contentDocument || ifr.document;
            var height = calcPageHeight(iDoc)
            if(height < 500){
                height = 500;
            }
            ifr.style.height = height + 'px';
        }
    }

    // $("#J_iframe").on('load',function(){
    //     var iframe=this;
    //     if (iframe) {
    //         var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
    //         debugger;
    //         if (iframeWin.document.body) {
    //             iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
    //         }
    //     }
    // });
    //左侧菜单点击拼装面包屑
    var selected = $(".J_menuItem.select");
    //菜单点击处理面包屑
    $(".J_menuItem").on('click',function(){
        var url = $(this).attr('href');
        $("#J_iframe").attr('src',url);
        // $("#Body-main").load(url);
        if(selected){
            selected.removeClass("select");
        }
        $(this).addClass("select");
        selected = $(this);
        //判断是几级菜单
        var breadcrumbHTML=new Array();
        breadcrumbHTML.push("<li class=\"\"><a href=\"#\">首页</a></li>");
        var parent =$(this).parent().parent();
        var parentClazz=parent.attr("class");
        if(parentClazz.indexOf("nav-third-level")>-1){
            //三级菜单

            var appName= parent.parent().parent().parent().prevAll(".hidden-folded:first").children().html()

            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push(appName);
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");
            var firstMenu = parent.parent().parent().parent().children(":first").children(".nav-label").html();

            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push(firstMenu);
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");
            var secondMenu = parent.parent().children("a").html().replace("<span class=\"fa arrow\"></span>","");

            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push(secondMenu);
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");

            breadcrumbHTML.push("/");
            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push($(this).html());
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");

        }else if(parentClazz.indexOf("nav-second-level")>-1){
            //二级菜单
            var appName= parent.parent().prevAll(".hidden-folded:first").children().html()


            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push(appName);
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");
            var firstMenu = parent.parent().children(":first").children(".nav-label").html();

            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push(firstMenu);
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");

            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push($(this).html());
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");

        }else if(parentClazz=="nav"&&$(this).html().indexOf("首页")==-1){
            // alert($(this).html())
            //一级菜单
            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push($(this).parent().prevAll(".hidden-folded:first").children(":first").html());
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");


            breadcrumbHTML.push("<li class=\"\">");
            breadcrumbHTML.push("<a href=\"#\">");
            breadcrumbHTML.push($(this).children(".nav-label").html());
            breadcrumbHTML.push("</a>");
            breadcrumbHTML.push("</li>");
        }else{
            //"首页";
        }
        //组装面包屑HTML
        $("#breadcrumb").html(breadcrumbHTML.join(""));
        return false;
    });

})

// 计算页面的实际高度，iframe自适应会用到
function calcPageHeight(doc) {
    try{

        var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
        var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)
        var height  = Math.max(cHeight, sHeight)
        return height
    }catch (e) {
        return 1000;
    }
}


function NavToggle() {
    $('.navbar-minimalize').trigger('click');
}

function SmoothlyMenu() {
    if (!$('body').hasClass('mini-navbar')) {
        $('#side-menu').hide();
        setTimeout(
            function () {
                $('#side-menu').fadeIn(500);
            }, 100);
    } else if ($('body').hasClass('fixed-sidebar')) {
        $('#side-menu').hide();
        setTimeout(
            function () {
                $('#side-menu').fadeIn(500);
            }, 300);
    } else {
        $('#side-menu').removeAttr('style');
    }
}